<?php 
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : product.class.php   FILE_PATH : \\classes\application\product.class.php
 * ....商品管理
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Aug 22 14:37:46 CST 2007
 */
defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

include_once("member.class.php");

class ProductClass extends FrameWork {
	/**
	 * 生成商品列表数组方法
	 *
	 * @param object $obj_condition 查询条件对象
	 * @param object $obj_page	 	翻页对象
	 * @return array $product_array 返回商品列表数组
	 */
	function getProductList($obj_condition, &$obj_page,$type='simple'){
		//构造查询条件
		$condition_str = $this->_conditionSearch($obj_condition);
		switch ($type){
			case 'simple'://取商品列表
				$product_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "product");
				break;
			case 'member'://连表 与member和shop
				$product_array = $GLOBALS['db']->GetJoinList($obj_page,
				array('product','member','shop_info','area','member_extend'),
				"left join",
				array("product.member_id=member.member_id","product.member_id=shop_info.member_id","product.p_area_id=area.area_id","product.member_id=member_extend.member_id"),
				array('*','login_name as shopmaster','audit_state','area_name','mobilephone,phone,QQ,MSN,SKYPE,TAOBAO,personal_certify'),
				$condition_str,
				0
				);	
				break;
		}
		
		//取缩略图
		if (is_array($product_array)){
			for ($i=0;$i<count($product_array);$i++){
				//判断是否有商品图片
				if (file_exists(BasePath.'/'.$product_array[$i]['p_pic'])){
					//将图片名称拆开，并加缩略图后缀，判断是否存在
					$line = explode('.',$product_array[$i]['p_pic']);
					if (file_exists(BasePath.'/'.$line[0].'_small.'.$line[1]) && file_exists(BasePath.'/'.$line[0].'_big.'.$line[1])){
						$product_array[$i]['small_pic'] = $line[0].'_small.'.$line[1];
						$product_array[$i]['mid_pic'] = $line[0].'_mid.'.$line[1];
						$product_array[$i]['big_pic']    = $line[0].'_big.'.$line[1];
					}else {
						$product_array[$i]['small_pic'] = $product_array[$i]['p_pic'];
						$product_array[$i]['mid_pic'] = $product_array[$i]['p_pic'];
						$product_array[$i]['big_pic']    = $product_array[$i]['p_pic'];
					}
					unset($line);
				}
			}
		}

		return $product_array;
	}
	/**
	 * 获得一条商品记录
	 *
	 * @param int $product_id 商品ID
	 * @return array 返回商品记录数组
	 */
	function getProductRow($product_id){
		$product_row = $GLOBALS['db']->GetOneRow($product_id,'product','p_code');
		return $product_row;
	}
	/**
	 * 获得一条商品记录
	 *
	 * @param int $product_id 商品ID
	 * @return array 返回商品记录数组
	 */
	function getProductRowById($product_id){
		$product_row = $GLOBALS['db']->GetOneRow($product_id,'product','p_id');
		return $product_row;
	}
	
	/**
	 * 获得商品最大ID
	 *
	 * @return int $product_last_id
	 */
	function getProductLastId(){
		$product_row = $GLOBALS['db']->GetLastRow("product", "p_id");
		return $product_row['p_id'];
	}
	
	/**
	 * 添加商品
	 *
	 * @param array $input
	 * @return boolean
	 */
	function addProduct($input){
		$valueArray = array();
		$valueArray["p_name"]		= $input["txtPname"];
		$valueArray["pc_id"]		= $input["slPCId"];
		$valueArray["theme_id"]		= $input["txtThemeid"];
		$valueArray["member_id"]	= $input["txtMemberid"];
		$valueArray["p_code"]		= $input["txtPcode"];
		$valueArray["p_price"]		= $input["txtPprice"];
		$valueArray["p_point"]		= $input["txtPpoint"];
		$valueArray["p_view_num"]	= $input["txtPviewnum"];
		if ($input["txtPstarttime"] != ''){
			$valueArray["p_start_time"] = $input["txtPstarttime"];
		}
		$valueArray["p_valid_days"]	= $input["slValiddays"];
		if ($input["txtPendtime"] != ''){
			$valueArray["p_end_time"]	= $input["txtPendtime"];
		}
		if (isset($input["txtPclassid"])) {
			$valueArray["p_class_id"]	= $input["txtPclassid"];	
		}
		$valueArray["p_genuine"] = !empty($input["txtPgenuine"]) ? $input["txtPgenuine"] : 0;
		$valueArray["p_7day_return"] = !empty($input["txtP7day"]) ? $input["txtP7day"] : 0;
		$valueArray["p_storage"]	= $input["txtPstorage"];
		$valueArray["p_state"]		= $input["txtPstate"];
		$valueArray["p_pic"]		= $input["txtPpic"];
		$valueArray["p_intro"]		= $input["txtPinfo"];
		$valueArray["p_add_time"] = time();
		$valueArray["p_update_time"] = time();
		$valueArray["p_auto_publish"] = $input["chxAutopublish"];
		$valueArray["p_sell_type"]	= $input["radioSelltype"];
		/*如果是拍卖,则当前起拍价为初始价格*/
		if ($input["radioSelltype"] == '0') {
			$valueArray["p_cur_price"] = $input["txtPprice"];
		}
		$valueArray["p_type"]		= $input["radioType"];
		if ($input["txtGroupprice"] != ''){
			$valueArray["p_group_price"] = $input["txtGroupprice"];
			$valueArray["p_group_mincount"]	= $input["txtGroupmincount"];
		}
		$valueArray["p_system_step"] = $input["system_step"];
		$valueArray["p_price_step"] = $input["price_step"];
//		$valueArray["p_province"]	= $input["slProvince"];
//		$valueArray["p_city"]   = $input["slCity"];
		$valueArray["p_transfee_charge"] = $input["radioTransfee"];
		$valueArray["p_tf_py"]		= $input["pyTF"];
		$valueArray["p_tf_kd"]		= $input["kdTF"];
		$valueArray["p_tf_ems"]		= $input["emsTF"];
		$valueArray["p_have_invoices"] = $input["radioInvoices"];
		$valueArray["p_have_warranty"] = $input["radioWarranty"];
		$valueArray["p_recommended"] = $input["chxRecommended"];
		$valueArray["p_remark"]		= $input["txtRemark"];
		$valueArray["p_pay_method"]		= $input["pay_method"];
		$valueArray["p_currency_category"]		= $input["p_currency_category"];
		$valueArray["p_ifnopub"]		= intval($input["IfnoPub"]);
		if ($input['pay_predeposit'] != ''){
			$valueArray["p_predeposit_state"]		= $input["pay_predeposit"];
		}
		$valueArray["p_area_id"]		= $input["area_id"];
		if ($input['brand_id'] != ''){
			$valueArray["p_pb_id"]		= $input["brand_id"];
		}
		$valueArray["p_keywords"]		= $input["txtKeywords"];
		$valueArray["p_description"]		= $input["txtDescription"];
		
		$insert_rs = $GLOBALS['db']->InsertRow($valueArray, 'product', 'p_id');
		return $insert_rs;
	}
	/**
	 * 编辑商品
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProduct($input){
		$valueArray = array();
		$valueArray["p_name"]		= $input["txtPname"];
		$valueArray["pc_id"]		= $input["slPCId"];
		$valueArray["theme_id"]		= $input["txtThemeid"];
		//		$valueArray["shop_id"]		= $input["txtShopid"];
		$valueArray["p_price"]		= $input["txtPprice"];
		$valueArray["p_point"]		= $input["txtPpoint"];
		if ($input["txtPstarttime"] != '') {
			$valueArray["p_start_time"] = $input["txtPstarttime"];
		}
		if ($input["txtPendtime"] != '') {
			$valueArray["p_end_time"]	= $input["txtPendtime"];
		}
		if ($input["txtPstate"] != '') {
			$valueArray["p_state"]		= $input["txtPstate"];
		}
		$valueArray["p_valid_days"]	= $input["slValiddays"];
		if (isset($input["txtPclassid"])) {
			$valueArray["p_class_id"]	= $input["txtPclassid"];	
		}
		$valueArray["p_genuine"] = !empty($input["txtPgenuine"]) ? $input["txtPgenuine"] : 0;
		$valueArray["p_7day_return"] = !empty($input["txtP7day"]) ? $input["txtP7day"] : 0;		
		$valueArray["p_storage"]	= $input["txtPstorage"];
		if ($input["txtPpic"] != ''){
			$valueArray["p_pic"]		= $input["txtPpic"];
		}
		$valueArray["p_intro"]		= $input["txtPinfo"];
		$valueArray["p_update_time"] = time();
		$valueArray["p_auto_publish"] = $input["chxAutopublish"];
		$valueArray["p_sell_type"]	= $input["radioSelltype"];
		$valueArray["p_type"]		= $input["radioType"];
		if ($input["txtGroupprice"] != ''){
			$valueArray["p_group_price"] = $input["txtGroupprice"];
			$valueArray["p_group_mincount"]	= $input["txtGroupmincount"];
		}
		$valueArray["p_cur_price"] 	 = $input["txtPprice"];
		$valueArray["p_system_step"] = $input["system_step"];
		$valueArray["p_price_step"] = $input["price_step"];
//		$valueArray["p_province"]	= $input["slProvince"];
//		$valueArray["p_city"]		= $input["slCity"];
		$valueArray["p_transfee_charge"] = $input["radioTransfee"];
		$valueArray["p_tf_py"]		= $input["pyTF"];
		$valueArray["p_tf_kd"]		= $input["kdTF"];
		$valueArray["p_tf_ems"]		= $input["emsTF"];
		$valueArray["p_have_invoices"] = $input["radioInvoices"];
		$valueArray["p_have_warranty"] = $input["radioWarranty"];
		$valueArray["p_recommended"] = $input["chxRecommended"];
		$valueArray["p_remark"]		= $input["txtRemark"];
		$valueArray["p_pay_method"]		= $input["pay_method"];
		$valueArray["p_currency_category"]		= $input["p_currency_category"];
		$valueArray["p_ifnopub"]		= intval($input["IfnoPub"]);
		if ($input['pay_predeposit'] != ''){
			$valueArray["p_predeposit_state"]		= $input["pay_predeposit"];
		}
		$valueArray["p_area_id"]		= $input["area_id"];
		if ($input['brand_id'] != ''){
			$valueArray["p_pb_id"]		= $input["brand_id"];
		}
		$valueArray["p_keywords"]		= $input["txtKeywords"];
		$valueArray["p_description"]		= $input["txtDescription"];
		
		$update_rs = $GLOBALS['db']->UpdateRow($input["txtPid"], $valueArray, 'product', 'p_code');
		return $update_rs;
	}
	/**
	 * 更新商品数量
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProductCount($input){
		$valueArray = array();
		$valueArray["p_storage"]	= $input["txtPcount"];
		$update_rs = $GLOBALS['db']->UpdateRow($input["txtPcode"], $valueArray, 'product', 'p_code');
		return true;
	}
	/**
	 * 更新拍卖商品当前价格及拍卖次数
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProductCurPrice($input){
		$valueArray = array();
		$valueArray["p_cur_price"]	= $input["cur_price"];
		/*更新拍卖次数*/
		if ($input['ifupdatebidnum']==1){
			$valueArray["p_bid_num"]	= "p_bid_num+1";
		}
	
		$update_rs = $GLOBALS['db']->UpdateRow($input["p_code"], $valueArray, 'product', 'p_code','formula');
		return true;
	}
	
	/**
	 * 更新拍卖商品拍卖次数，主要针对多件商品拍卖
	 * 
	 * @param array $input
	 * @return boolean
	 */
	function updateProductBidCount($input){
		$valueArray = array();
		
		$valueArray["p_bid_num"]	= intval($input['bid_num']);
		
		$update_rs = $GLOBALS['db']->UpdateRow($input["p_code"], $valueArray, 'product', 'p_code','formula');
		return true;
	}
	
	/**
	 * 更新商品当期售出数量和累计售出数量
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProductSoldCount($input){
		$value_array = array();
		if($input["have_sold_all"] == 1){//库存为0
			$is_zero = true;//库存为0，下架
			$value_array['p_sold_sum'] = 'p_sold_sum+'.intval($input["txtBuyNum"]);
		}else{
			$is_zero = false;
			$value_array['p_sold_num'] = 'p_sold_num+'.intval($input["txtBuyNum"]);
			$value_array['p_sold_sum'] = 'p_sold_sum+'.intval($input["txtBuyNum"]);
		}
		if($input["decrease_num"] != ""){
			$value_array['p_storage'] = 'p_storage-'.intval($input["decrease_num"]);
		}

		$result = $GLOBALS['db']->UpdateRow($input["p_code"],$value_array,'product','p_code', true);
		unset($value_array);
		if ($is_zero === true){
			$value_array = array();
			$value_array['p_state'] = '0';//下架
			$value_array['p_sold_num'] = '0';
			$result = $GLOBALS['db']->UpdateRow($input["p_code"],$value_array,'product','p_code');
		}
		return $result;
	}
	/**
	 * 更新商品浏览次数
	 *
	 * @param unknown_type $input
	 * @return unknown
	 */
	function updateProductViewNum($input){
		$value_array = array();
		$value_array['p_view_num'] = 'p_view_num+'.intval($input["txtViewNum"]);
		$result = $GLOBALS['db']->UpdateRow($input["p_code"],$value_array,'product','p_code', true);
		return $result;
	}

	/**
	 * 更新商品订单状态
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateProductOrderState($input){
		$valueArray = array();
		$valueArray["sp_state"]	= $input["txtSPstate"];
		$update_rs = $GLOBALS['db']->UpdateRow($input["txtSPcode"], $valueArray, 'product_sold', 'sp_code');
		return true;
	}
	/**
	 * 删除商品
	 *
	 * @param array $input
	 * @return boolean
	 */
	function delProduct($input){ 
		if ($input != ''){
			$product_info = $this->getProductRow($input);
			$temp = explode('.',$product_info);
			if ($product_info['p_pic'] != ""){
				@unlink("../".$product_info['p_pic']);
				@unlink("../".$temp[0].'_small.'.$temp[1]);
				@unlink("../".$temp[0].'_big.'.$temp[1]);
			}
			$del_rs = $GLOBALS['db']->DelRow($input, 'product', 'p_code');
			if ($del_rs == true) {
				$this->delProductAttribute($input);
			}
			return $del_rs;
		}else {
			return false;
		}
	}
	/**
	 * 删除商品属性内容
	 * 
	 * @param array $input
	 * @return boolean
	 */
	function delProductAttribute($input){
		if (!empty($input)) {
			$del_rs = $GLOBALS['db']->DelRow($input, 'product_attributes', 'p_id');
			if ($del_rs !== true) {
				return false;
			}
			return true;
		}
		return false;
	}

	/**
	 * 添加商品属性内容
	 *
	 * @param string $pid 商品编码
	 * @param array $input 属性数组，形式为 $input = array("2|12,13,14", "3|15,16", "4|17");其中"|"前为属性ID，"|"后为属性内容ID字符串
	 * @return boolean
	 */
	function addProductAttribute($pid, $input){
		$valueArray = array();
		foreach ($input as $key=>$value ){
			$attribute = explode('|',$value);
			$valueArray["p_id"]	= $pid;
			$valueArray["a_id"]	= $attribute[0];
			$valueArray["pac_content"]	= $attribute[1];
			$insert_rs = $GLOBALS['db']->InsertRow($valueArray, 'product_attributes', 'pac_id');
		}
		return true;
	}
	/**
	 * 更新商品属性内容
	 *
	 * @param string $pid 商品编号
	 * @param array $input 商品属性数组
	 * @return boolean
	 */
	function updateProductAttribute($pid, $input){
		if (trim($pid) != ''){
			$del_rs = $GLOBALS['db']->DelRow($pid, 'product_attributes', 'p_id');
			$valueArray = array();
			foreach ($input as $key=>$value ){
				$attribute = explode('|',$value);
				$valueArray["p_id"]	= $pid;
				$valueArray["a_id"]	= $attribute[0];
				$valueArray["pac_content"]	= $attribute[1];
				$insert_rs = $GLOBALS['db']->InsertRow($valueArray, 'product_attributes', 'pac_id');
			}
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 获得商品属性
	 *
	 * @param unknown_type $condition_str
	 * @param unknown_type $obj_page
	 * @return unknown
	 */
	function getProductAttribute($condition_str, &$obj_page){
		$product_attribute_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "product_attributes");
		return $product_attribute_array;
	}

	/**
	 * 获得商品订单最大ID
	 *
	 * @return int $product_order_last_id
	 */
	function getProductOrderLastId(){
		$product_order_row = $GLOBALS['db']->GetLastRow('product_sold','sp_id','');
		$product_order_last_id = $product_order_row['sp_id'];
		return $product_order_last_id;
	}


	/**
	 * 构造商品检索条件
	 *
	 * @param object $obj_condition 查询条件对象
	 * @return string $conditionStr 返回查询条件SQL字符串
	 */
	function _conditionSearch($obj_condition){
		$conditionStr = "";

		/*商品ID*/
		if ($obj_condition['p_id'] != ''){
			$conditionStr .= " and product.p_id ='" . $obj_condition['p_id'] . "'";
		}
		/*模糊搜索商品名*/
		if ($obj_condition['keygenre'] == '1' && $obj_condition['key'] != ''){
			$obj_condition['key'] = str_replace('　',' ',$obj_condition['key']);
			$obj_condition['key'] = explode(' ',trim($obj_condition['key']));
			if (is_array($obj_condition['key'])){
				$line = " and ( ";
				foreach ($obj_condition['key'] as $k => $v){
					if (trim($v) != ''){
						$line .= " product.p_name like '%" . $v . "%' or ";
					}
				}
				if ($line != ' and ( '){
					$conditionStr .= substr(rtrim($line),0,strlen(rtrim($line))-3).')';
				}
				unset($line);
			}else {
				$conditionStr .= " and product.p_name like '%" . $obj_condition['key'] . "%'";
			}
		}
		/*商品编号*/
		if ($obj_condition['p_code'] != ''){
			$conditionStr .= " and product.p_code ='" . $obj_condition['p_code'] . "'";
		}
		/*
		if($obj_condition[keygenre] == "3" && $obj_condition[key] != ""){
		$conditionStr .= " and pc_id = '" . $obj_condition[key] . "'";
		}*/
		/*搜索商品分类*/
		if ($obj_condition['search_cate'] != '' && $obj_condition['search_cate'] != '0'){

			$conditionStr .= " and (product.pc_id = '" . $obj_condition['search_cate'] . "'";

			/*搜索子分类*/
			require_once("productclass.class.php");
			$obj_search_cate = new ProductCategoryClass();
			$class_array = $obj_search_cate->listClassDetail();
			if(!is_array($class_array)){
				$class_array = array();
			}
			$id = $obj_condition['search_cate'];
			$result = array();
			$search_cate = $obj_search_cate->getArrayById($class_array,$result,$id);
			if("" != $search_cate){
				$category_sql = "";
				foreach ($search_cate as $value){
					$category_sql .= " or product.pc_id = '" . $value['id'] . "'";
				}
			}
			$conditionStr .= $category_sql . ")";
			unset($obj_search_cate);
		}
		/*按价格搜索*/
		if ($obj_condition['price_min'] != ''){
			$conditionStr .= " and product.p_price >= '" . $obj_condition['price_min'] . "'";
		}
		if ($obj_condition['price_max'] != ''){
			$conditionStr .= " and product.p_price <= '" . $obj_condition['price_max'] . "'";
		}
		/*搜索交易类型*/
		if ($obj_condition['sell_type'] != ''){
			if(is_array($obj_condition['sell_type'])){
				$conditionStr .= " and (";
				foreach ($obj_condition['sell_type'] as $value){
					$condition .= " or product.p_sell_type = '" . intval($value) . "'";
				}
				$conditionStr .= substr($condition, 3).")";

			}else{
				$conditionStr .= " and product.p_sell_type = '" . intval($obj_condition['sell_type']) . "'";
			}
		}
		/*搜索新旧程度*/
		if ($obj_condition['p_type'] != ''){
			if(is_array($obj_condition['p_type'])){
				$conditionStr .= " and (";
				foreach ($obj_condition['p_type'] as $value){
					$condition .= " or product.p_type = '" . intval($value) . "'";
				}
				$conditionStr .= substr($condition, 3).")";

			}else{
				$conditionStr .= " and product.p_type = '" . intval($obj_condition['p_type']) . "'";
			}

		}
		/*搜索运费由卖家支付*/
		if ($obj_condition['tf_charge'] == '0'){
			$conditionStr .= " and product.p_transfee_charge = '" . $obj_condition['tf_charge'] . "'";
		}
		/*搜索商品新旧程度*/
		if ($obj_condition['type'] != ''){
			$conditionStr .= " and product.p_type = '" . $obj_condition['type'] . "'";
		}
		/*是否自动发货*/
		if ($obj_condition['autopub'] != ''){
			$conditionStr .= " and product.p_auto_publish = '" . $obj_condition['autopub'] . "'";
		}
		/*搜索某些卖家*/
		if($obj_condition['shops'] != ''){
			$conditionStr .= " and product.member_id in " . $obj_condition['shops'] . "";
		}
		/*搜索某个卖家*/
		if($obj_condition['member'] != ''){
			$conditionStr .= " and product.member_id = " . $obj_condition['member'] . "";
		}
		/*搜索天数之前结束的商品*/
		if ($obj_condition['end_time'] != ''){
			$search_end_time = time()+($obj_condition['end_time']+1)*86400;
			$conditionStr .= " and product.p_end_time <= '" . $search_end_time . "'";
		}
		/*搜索到现在时间结束的*/
		if ($obj_condition['now_end'] != ''){
			$conditionStr .= " and product.p_end_time <= '" . time() . "'";
		}
		/*搜索某个时间段*/
		if ($obj_condition['start_date'] != ''){
			$conditionStr .= " and product.p_add_time >= '" . $obj_condition['start_date'] . "'";
		}
		if ($obj_condition['end_date'] != ''){
			$conditionStr .= " and product.p_add_time <= '" . $obj_condition['end_date'] . "'";
		}
		//商品状态
		if ($obj_condition['state'] != '' && $obj_condition['state'] != 'none'){
			$conditionStr .= " and product.p_state = '" . $obj_condition['state'] . "'";
		}elseif ($obj_condition['state'] == 'none'){
			$conditionStr .= "";
		}else{
			$conditionStr .= " and product.p_state = '1'";
		}
		/*橱窗推荐*/
		if ($obj_condition['recommended'] != ''){
			$conditionStr .= " and product.p_recommended = '" . $obj_condition['recommended'] . "'";
		}
		/*店铺推荐*/
		if ($obj_condition['store_recommended'] != ''){
			$conditionStr .= " and product.p_store_recommended = '" . $obj_condition['store_recommended'] . "'";
		}
		/**
		 * 搜索多个指定ID的商品
		 */
		if(is_array($obj_condition['pcodes'])){
			foreach ($obj_condition['pcodes'] as $value){
				$condition_pcode .= " or product.p_code = '" . $value . "'";
			}
			if(substr(trim($condition_pcode),0,2)=="or"){
				$conditionStr .= " and (" . substr(trim($condition_pcode),2) . ") ";
			}
		}
		/*搜索门店类别*/
		if ($obj_condition['p_class_id'] != ''){
			$conditionStr .= " and product.p_class_id = '" . $obj_condition['p_class_id'] . "'";
		}
		/*是否主页显示*/
		if ($obj_condition['p_index'] != ""){
			$conditionStr .= " and product.p_index = '" . $obj_condition['p_index'] . "'";
		}
		/**
		 * 判断管理员所在权限组
		 */
		if ($_SESSION['s_admin']['id'] != ''){
			/**
			 * 创建管理员对象
			 */
			if (!is_object($this->obj_sys_member)) {
				require_once("system_power.class.php");
				$this->obj_sys_member = new SystemPowerClass();
			}
			$sys_member_info = $this->obj_sys_member->getSysMemberById($_SESSION['s_admin']['id']);
			$sys_group_info  = $this->obj_sys_member->getSysGroupById($sys_member_info['sys_group_id']);
			if ($sys_group_info['sys_group_area_id']){
				$obj_condition['p_area_id'] = $sys_group_info['sys_group_area_id'];
			}
		}
		
		//地区ID
		if ($obj_condition['p_area_id'] != ""){
			//取该地区下的所属地区
			if (!is_object($obj_area)){
				require_once("area.class.php");
				$obj_area = new AreaClass();
			}
			$condition_area['area_path_like'] = ','.$obj_condition['p_area_id'].',';
			$area_list = $obj_area->getArea($condition_area);
			if (is_array($area_list)){
				foreach ($area_list as $k => $v){
					$area_line .= " or product.p_area_id = '" . $v['area_id'] . "'";
				}
			}
			$conditionStr .= " and (product.p_area_id = '" . $obj_condition['p_area_id'] . "'";
			$conditionStr .= $area_line;
			$conditionStr .= ')';
			unset($obj_area,$area_line);
		}
		
		//品牌
		if ($obj_condition['p_pb_id'] != ""){
			//取该品牌下的所属品牌
			if (!is_object($obj_product_brand)){
				require_once("product_brand.class.php");
				$obj_product_brand = new ProductBrandClass();
			}
			$condition_brand['pb_path_like'] = ','.$obj_condition['p_area_id'].',';
			$brand_list = $obj_product_brand->getProductBrand($condition_brand,$page);
			if (is_array($brand_list)){
				foreach ($brand_list as $k => $v){
					$brand_line .= " or product.p_pb_id = '" . $v['pb_id'] . "'";
				}
			}
			$conditionStr .= " and (product.p_pb_id = '" . $obj_condition['p_pb_id'] . "'";
			$conditionStr .= $brand_line;
			$conditionStr .= ')';
			unset($obj_product_brand,$brand_line);
		}
		
		
		/*列表排序方式*/
		if ($obj_condition['order'] != 'no'){
			$conditionStr .= " order by";
			if($obj_condition['order'] == 1){
				$conditionStr .= " product.p_end_time";
			}elseif($obj_condition['order'] == 2){
				$conditionStr .= " product.p_price ";
			}elseif($obj_condition['order'] == 3){
				$conditionStr .= " product.p_add_time ";
			}elseif($obj_condition['order'] == 4){
				$conditionStr .= " product.p_sold_num ";
			}elseif($obj_condition['order'] != ''){
				$conditionStr .= " product.".$obj_condition['order'];
			}else{
				$conditionStr .= " product.p_end_time";
			}
			if($obj_condition['sorttype'] == 1){
				$conditionStr .= " ";
			}else{
				$conditionStr .= " desc";
			}
		}
		//指定数量，当分页为空时才可以使用
		if ($obj_condition['limit_num'] != ''){
			$obj_condition['limit_start_num'] = intval($obj_condition['limit_start_num']) > 0?intval($obj_condition['limit_start_num']):'0';
			$conditionStr .= " limit ".$obj_condition['limit_start_num'].",".intval($obj_condition['limit_num']);
		}
		return $conditionStr;

	}

	/**
	 * 生成商品列表数组方法
	 *
	 * @param object $record_count 获取的条数
	 * @param object $condition	 	条件
	 * $orderby 排序依靠字段
	 * @return array $product_array 返回商品列表数组
	 */
	function getSomeProductList($record_count,$condition = "",$orderby = ""){
		$product_array = $GLOBALS['db']->GetList($condition . " order by $orderby p_update_time desc limit 0," . $record_count, $obj_page, "product","p_id,p_name,p_code,p_price,p_pic,pc_id");
		return $product_array;
	}


	/**
	 * 更新商品的店铺宝贝分类ID
	 *
	 * @param array $input
	 * @return boolean
	 */
	function updateShopProductCategory($input){
		$valueArray = array();
		if(is_array($input["chboxPid"])){
			foreach ($input["chboxPid"] as $value){
				$valueArray["p_class_id"] = $input["shoppcid"];
				$update_rs = $GLOBALS['db']->UpdateRow($value, $valueArray, 'product', 'p_code');
			}
		}else{
			$valueArray["p_class_id"] = $input["shoppcid"];
			$update_rs = $GLOBALS['db']->UpdateRow($input["chboxPid"], $valueArray, 'product', 'p_code');
		}

		return true;
	}
	/**
	 * 商品上下架
	 *
	 * @param array $input
	 * @param int $state 0下架 1上架
	 * @return boolean
	 */
	function changeProductState($input, $state){
		$valueArray = array();
		if(is_array($input["chboxPid"])){
			foreach ($input["chboxPid"] as $value){
				$valueArray["p_state"] = $state;
				if($state=="1"){//上架
					$valueArray["p_start_time"] = $input["p_start_time"];
					$valueArray["p_end_time"] = $input["p_end_time"];
					$valueArray["p_valid_days"] = $input["p_valid_days"];
					$valueArray["p_sold_num"] = $input["p_sold_num"];
					$valueArray["p_bid_num"] = 0;
					$valueArray["p_irregularities"] = $input["p_irregularities"];
					//判断是否有价格传过来，如果有，则更新当前拍卖价格
					if ($input['p_price'][$value] != ''){
						$valueArray["p_cur_price"] = $input['p_price'][$value];
					}
				}else{//下架
					$valueArray["p_recommended"] = $input["p_recommended"];
					$valueArray["p_auto_publish"] = '0';
				}
				$update_rs = $GLOBALS['db']->UpdateRow($value, $valueArray, 'product', 'p_code');
			}
		}else{
			$valueArray["p_state"] = $state;
			if($state=="1"){//上架
				$valueArray["p_start_time"] = $input["p_start_time"];
				$valueArray["p_end_time"] = $input["p_end_time"];
				$valueArray["p_valid_days"] = $input["p_valid_days"];
				$valueArray["p_sold_num"] = $input["p_sold_num"];
				$valueArray["p_bid_num"] = 0;
				$valueArray["p_irregularities"] = $input["p_irregularities"];
				//判断是否有价格传过来，如果有，则更新当前拍卖价格
				if ($input['p_price'] != ''){
					$valueArray["p_cur_price"] = $input['p_price'];
				}
			}else{//下架
				$valueArray["p_recommended"] = $input["p_recommended"];
				$valueArray["p_auto_publish"] = '0';
			}
			$update_rs = $GLOBALS['db']->UpdateRow($input["chboxPid"], $valueArray, 'product', 'p_code');
		}

		return true;
	}
	/**
	 * 商品违规属性操作
	 * @author workflow Develop Team
	 * @param  
	 * @return int/bool/object/array
	 */
	function changeProductIrregularities($input, $state){
		$valueArray = array();
		if(is_array($input["pid"])){
			foreach ($input["pid"] as $value){
				$valueArray["p_irregularities"] = $state;
				if ($state == 1){
					$valueArray["p_state"] = 0;
				}else {
					$valueArray["p_state"] = 1;
				}
				$update_rs = $GLOBALS['db']->UpdateRow($value, $valueArray, 'product', 'p_code');
			}
		}else{
			$valueArray["p_irregularities"] = $state;
			if ($state == 1){
				$valueArray["p_state"] = 0;
			}else {
				$valueArray["p_state"] = 1;
			}
			$update_rs = $GLOBALS['db']->UpdateRow($input["pid"], $valueArray, 'product', 'p_code');
		}

		return true;
	}

	/**
	 * 根据条件更新所有符合条件的商品记录
	 *
	 * @param array $update_array
	 * @param string $condition
	 */
	function updateProductInCondition(){
		/*整理需要下架的商品*/
		$update_array = array();
		$update_array["p_state"] = "0";
		$update_array["p_recommended"] = "0";
		$update_array["p_store_recommended"] = "0";
		$condition = " p_state = '1' and p_end_time < '" . time() . "'";
		$update_rs = $GLOBALS['db']->UpdateRows('product', $update_array, $condition);
		unset($update_array);
		
		/*整理需要重新上架的商品*/
		
		
		$update_array = array();
		$update_array["p_state"] = "'1'";
		$update_array["p_auto_publish"] = "'0'";
		$update_array["p_sold_num"] = "0";
		$update_array["p_bid_num"] = "0";
		$update_array["p_start_time"] = "p_end_time";
//		$update_array["p_end_time"] = "UNIX_TIMESTAMP(adddate(FROM_UNIXTIME(p_end_time),p_valid_days))";
		$update_array["p_end_time"] = "p_end_time+p_valid_days*24*24*60";
		$condition = " p_state = '0' and p_end_time <= '" . time() . "' and p_auto_publish = '1' and p_ifnopub = '0'";
		$update_rs = $GLOBALS['db']->UpdateRows('product', $update_array, $condition,"formula");
		unset($update_array);

		return true;
	}
	
	/**
	 * 更新设定上架时间的商品
	 *
	 * @param array $update_array
	 * @param string $condition
	 */
	function updatePubTimeProduct(){

		$update_array = array();
		$update_array["p_state"] = "1";
		$update_array["p_sold_num"] = "0";
		$update_array["p_ifnopub"] = "0";
		$update_array["p_bid_num"] = "0";
		
		$condition = " p_state = '0' and p_start_time <= '" . time() . "' and p_ifnopub='1'";
				
		$update_rs = $GLOBALS['db']->UpdateRows('product', $update_array, $condition);
		
		unset($update_array);

		return true;

	}
	
	/**
	 * 更新商品推荐状态
	 *
	 * @param array $input
	 */
	function updateProductRecommended($input){
		if(is_array($input['chboxPid'])){
			$update_array["p_recommended"] = $input['recommended'];
			foreach ($input['chboxPid'] as $value){
				if ($value != ''){
					$condition_str .= " or p_code ='" . $value . "'";
				}
			}
			$condition = substr($condition_str, 3);
			if ($condition != ''){
				$update_rs = $GLOBALS['db']->UpdateRows('product', $update_array, $condition);
			}
		}else{
			if ($input['chboxPid'] != ''){
				$update_array["p_recommended"] = $input['recommended'];
				$condition_str = " p_code ='" . $input['chboxPid'] . "'";
				$update_rs = $GLOBALS['db']->UpdateRows('product', $update_array, $condition);
			}
		}
		return true;
	}
	/**
	 * 更新商品店铺推荐状态
	 *
	 * @param array $input
	 */
	function updateProductShopRecommended($input){
		$update_array["p_store_recommended"] = $input['recommended'];
		if(is_array($input['chboxPid'])){
			foreach ($input['chboxPid'] as $value){
				$condition_str .= " or p_code ='" . $value . "'";
			}
			$condition = substr($condition_str, 3);
		}else{
			$condition = " p_code ='" . $input['chboxPid'] . "'";
		}
		$update_rs = $GLOBALS['db']->UpdateRows('product', $update_array, $condition);
		return true;
	}
	/**
	 * 更新商品统计信息
	 *
	 * @param unknown_type $member_id
	 * @param unknown_type $genre
	 * @return unknown
	 */
	function updateProductStatistics($member_id, $genre){
		switch($genre){
			case "sell":
				$state = 1;
				$condition = " member_id = '" . $member_id . "' and p_state = '" . $state . "'";
				$count = $GLOBALS['db']->CountRows('product', $condition);
				$value  = $count;  //发布商品数量
				break;
			case "recommend":
				$state = 1;
				$condition = " member_id = '" . $member_id . "' and p_recommended = '" . $state . "'";
				$count = $GLOBALS['db']->CountRows('product', $condition);
				$value  = $count;  //橱窗推荐商品数量
				break;
			case "both":
				$state1 = 1;
				$condition1 = " member_id = '" . $member_id . "' and p_state = '" . $state1 . "'";
				$count['sell'] = $GLOBALS['db']->CountRows('product', $condition1);
				$state2 = 1;
				$condition2 = " member_id = '" . $member_id . "' and p_recommended = '" . $state2 . "'";
				$count['recommend'] = $GLOBALS['db']->CountRows('product', $condition2);
				$value['sell']  = $count['sell'];  //橱窗推荐商品数量
				$value['recommend']  = $count['recommend'];  //橱窗推荐商品数量
				break;
		}
		require_once("member.class.php");
		$obj_member = new MemberClass();
		$obj_member->updateStatistics($member_id, $genre, $value);
		unset($obj_member);
		return true;

	}

	/**
	 * 更新商品属性，首页显示
	 * @author $input_param 更新数据
	 * @param  $sign更新标识
	 * @return 
	 */
	function updateProductIndex($input_param,$sign='p_code'){
		if (is_array($input_param)){
			$arr = array();
			foreach ($input_param as $k => $v){
				$arr[$k] = $v;
			}
		}
		return $GLOBALS['db']->UpdateRow($arr["$sign"], $arr, 'product', $sign);
	}

	/**
	 * 购买过此类商品的人还买过的商品列表
	 *   
	 * @param  $pcid 商品类别ID
	 * @return array 返回商品信息列表
	 */
	function getBoughtProduct($pcid)
	{

		$product = $GLOBALS['db']->GetList("and pc_id = '" . $pcid . "'",$obj_page,'product','p_code');//取该类下的所有商品
		if (is_array($product)){
			foreach ($product as $value){
				$product_array[] = $value['p_code'];
			}
		}
		$sold = $GLOBALS['db']->GetList("",$obj_page,'product_sold','p_code');//取售出的所有商品
		if (is_array($sold)){
			foreach ($sold as $value){
				$sold_array[] = $value['p_code'];
			}
		}

		$buyed = @array_intersect($sold_array,$product_array);//还买过的商品ID列表
		if (" " != $buyed)
		{
			$obj_condition[pcodes] = $buyed;
			$obj_condition[sorttype] = '';
			$obj_condition = $this->_conditionSearch($obj_condition);
			$result = $GLOBALS['db']->GetList($obj_condition ." limit 4",$obj_page,'product');//还买过的商品信息列表
		}

		return $result;
	}

	/**
	 * 竞拍中的宝贝
	 * @author workflow Develop Team
	 * @param  
	 * @return array
	 */
	function getAuctionList($condition,&$obj_page){
		$condition_str = $this->_conditionAuctionSearch($condition);
		$product = $GLOBALS['db']->GetList($condition_str,$obj_page,'bid');
		return $product;
	}

	/**
	 * 构造竞拍的商品检索条件
	 * @author workflow Develop Team
	 * @param  
	 * @return str
	 */
	function _conditionAuctionSearch($condition){
		$condition_str = "";
		/*出价状态*/
		if ($condition[bid_state] == 0){
			$condition_str .= " and bid_state='0' ";
		}elseif ($condition[bid_state] == 1){
			$condition_str .= " and bid_state='1' ";
		}elseif ($condition[bid_state] == 2){
			$condition_str .= " and bid_state='2' ";
		}
		/*会员ID*/
		if ($condition[bid_member_id] != ""){
			$condition_str .= " and bid_member_id='" . $condition[bid_member_id] . "' ";
		}
		/*商品名*/
		if ($condition[p_name] != ""){
			foreach ($condition[p_name] as $key => $value){
				if ($value[p_code] != ""){
					if ($key == 0){
						$condition_str .= " and bid_p_code='" . $value[p_code] . "' ";
					}else {
						$condition_str .= " or bid_p_code='" . $value[p_code] . "' ";
					}
				}
			}
		}
		/*商品分组*/
		if ($condition[group_p_code] != ''){
			$condition_str .= " group by bid_p_code ";
		}
		
		//排序
		if ($condition[order_by] != ''){
			$condition_str .= " order by ".$condition[order_by];
		}
		
		return $condition_str;
	}

	/**
	 * 取得商品统计
	 *
	 * @param string $condition
	 * @param int $row_nums
	 * @return array
	 */
	function getStaticsProduct($condition, $row_nums){
		$condition = $this->_conditionSearch($condition);
		$statics_array = $GLOBALS['db']->GetList($condition,$page,'product','count(*) as num, pc_id',$row_nums,'','pc_id');
		return $statics_array;
	}

	/**
	 * 更新商品的分类ID
	 * @author workflow Develop Team
	 * @param  
	 * @return int/bool/object/array
	 */
	function updateProductCategory($parentId,$new_id){
		$value_array = array();
		$value_array['pc_id'] = $new_id;
		$result = $GLOBALS['db']->UpdateRow($parentId,$value_array,'product','pc_id');
		return $result;
	}

	/**
	 * 判断是否调用静态页链接
	 * @author workflow Develop Team    
	 * @param  $product_array 商品列表	$ifhtml 是否使用静态页标识
	 * @return array
	 */
	function checkProductIfHtml($product_array,$ifhtml){
		if ($ifhtml == '1') {/*使用*/
			if (is_array($product_array)){
				foreach ($product_array as $k => $v){
					$product_array[$k] = $this->checkOneProductIfHtml($v,$ifhtml);
				}
			}
		}
		return $product_array;
	}

	/**
	 * 判断单独商品的是否存在静态页面的方法
	 * @author workflow Develop Team     
	 * @param  $product_array单条商品信息 $ifhtml 是否开启静态页面
	 * @return string
	 */
	function checkOneProductIfHtml($product_array,$ifhtml){
		if ($ifhtml == '1'){
			$html_url = "../html/user/".$product_array['pc_id']."/item_detail-".$product_array['p_code'].'.html';
			if (file_exists("../html/user/".$product_array['pc_id']."/item_detail-".$product_array['p_code'].'.html')){
				$product_array['html_url'] = $html_url;
			}
			unset($html_url);
		}
		return $product_array;
	}

	/**
	 * 按比例对商品图片进行缩放
	 * @author  $product_array
	 * $field 图片字段
	 * $length 缩放数值
	 * @param  
	 * @return 
	 */
	function productPicRatio($product_array,$field,$length){
		if (is_array($product_array)){
			foreach ($product_array as $i => $v){
				/*判断商品是否存在图片*/
				if (file_exists('../'.$product_array[$i][$field]) && $product_array[$i][$field] !=''){
					$product_array[$i]['ifpic'] = 1;
					$resize_array = Common::resizePic($product_array[$i][$field], $length);
					$product_array[$i]['small_pic'] = $resize_array['small_pic'];
					$product_array[$i]['mid_pic'] = $resize_array['mid_pic'];
					$product_array[$i]['big_pic'] = $resize_array['big_pic'];
					$product_array[$i]['width'] = $resize_array['width'];
					$product_array[$i]['height'] = $resize_array['height'];
				}

			}
		}
		return $product_array;
	}

	/**
	 * 商品图片入库
	 * @author workflow Develop Team     
	 * @param  $param
	 * @return bool
	 */
	function addProductPic($param){
		if (is_array($param)){
			$value_array = array();
			foreach ($param as $k => $v){
				if ($v != ''){
					$value_array[$k] = $v;
				}
			}
			$insert_rs = $GLOBALS['db']->InsertRow($value_array, 'product_pic', 'p_pic_id');
			return $insert_rs;
		}else {
			return false;
		}
	}

	/**
	 * 商品图片列表
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array
	 */
	function getProductPic($obj_condition,&$obj_page){
		$condition_str = $this->_conditionSearchPic($obj_condition);
		$pic_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "product_pic");
		return $pic_array;
	}

	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  $condition
	 * @return string
	 */
	function _conditionSearchPic($condition){
		$condition_str = '';

		if ($condition['p_code'] != ''){
			$condition_str .= " and p_code='" . $condition['p_code'] . "'";
		}
		if ($condition['p_pic'] != ''){
			$condition_str .= " and p_pic='" . $condition['p_pic'] . "'";
		}

		return $condition_str;
	}

	/**
	 * 删除图片
	 * @author workflow Develop Team     
	 * @param  $p_code商品编号
	 * @return bool
	 */
	function delProductPic($p_code){
		if (trim($p_code) != ''){
			$del_rs = $GLOBALS['db']->DelRow($p_code, 'product_pic', 'p_code');
			return $del_rs;
		}else {
			return false;
		}
	}
	/**
	 * 删除图片
	 * @author workflow Develop Team     
	 * @param  $p_pic图片字段内容
	 * @return bool
	 */
	function delProductPicByPic($p_pic){
		if (trim($p_code) != ''){
			$del_rs = $GLOBALS['db']->DelRow($p_pic, 'product_pic', 'p_pic');
			return $del_rs;
		}else {
			return false;
		}
	}

	/**
	 * 添加商品图片缓存数据
	 * @author workflow Develop Team     
	 * @param  $param
	 * @return bool
	 */
	function addProductPicCache($param){
		if (is_array($param)){
			$value_array = array();
			foreach ($param as $k => $v){
				if ($v != ''){
					$value_array[$k] = $v;
				}
			}
			$insert_rs = $GLOBALS['db']->InsertRow($value_array, 'product_pic_cache', 'p_pic_c_id');
			return $insert_rs;
		}else {
			return false;
		}
	}

	/**
	 * 删除商品图片缓存
	 * @author workflow Develop Team     
	 * @param  $p_pic_cache 图片名称
	 * @return bool
	 */
	function delProductPicCache($p_pic_cache){
		if (trim($p_pic_cache) != ''){
			$del_rs = $GLOBALS['db']->DelRow($p_pic_cache, 'product_pic_cache', 'p_pic_cache');
			return $del_rs;
		}else {
			return false;
		}
	}

	/**
	 * 清除图片缓存，并且删除废弃图片
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function cleanPicCache(){
		//缓存图片列表
		$pic_cache_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "product_pic_cache");
		//图片列表
		$pic_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "product_pic");
		//筛选需要删除的图片
		if (is_array($pic_cache_array)){
			foreach ($pic_cache_array as $k => $v){
				$is_exists = false;
				if (is_array($pic_array)){
					foreach ($pic_array as $k2 => $v2){
						if ($v['p_pic_cache'] == $v2['p_pic']){//说明图片是有效的不删除
							$is_exists = true;
							break;
						}
					}
					if ($is_exists == false){
						$del_array[] = $v['p_pic_cache'];
					}
				}
				//删除数据
				$this->delProductPicCache($v['p_pic_cache']);
			}
		}
		unset($pic_cache_array,$pic_array);
		//删除废弃的图片
		if (is_array($del_array)){
			foreach ($del_array as $k => $v){
				$arr = @explode('.',$v);
				@unlink(BasePath.'/'.$arr[0].'.'.$arr[1]);
				@unlink(BasePath.'/'.$arr[0].'_small.'.$arr[1]);
				@unlink(BasePath.'/'.$arr[0].'_big.'.$arr[1]);
				@unlink(BasePath.'/'.$arr[0].'_mid.'.$arr[1]);
				unset($arr);
			}
		}
		unset($del_array);
		return true;
	}
	
	/**
	 * 更新商品地区内容
	 *
	 * @param array $input
	 */
	function updateProductArea($input){
		$result = $GLOBALS['db']->UpdateRow($input['p_code'],$input, 'product', 'p_code');
		return $result;
	}

	/**
	 * 商品csv导入 更新图片信息
	 *
	 * @param array $update_array
	 * @param bool 布尔形式的返回结果
	 */
	function updateProductPicCSV($update_array){
		//商品表
		$value_array = array();
		$value_array["p_pic"] = $update_array['update_pic'];
		$condition = " member_id = '".$update_array['member_id']."' and p_pic = '" . $update_array['p_pic'] . "'";
		$update_rs = $GLOBALS['db']->UpdateRows('product', $value_array, $condition);
		unset($value_array);
		//商品图片表
		$value_array = array();
		$value_array["p_pic"] = $update_array['update_pic'];
		$condition = " p_pic = '" . $update_array['p_pic'] . "'";
		$update_rs = $GLOBALS['db']->UpdateRows('product_pic', $value_array, $condition);
		unset($value_array);
		return true;
	}
}
?>